<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/11
 * Time: 0:14
 */

namespace app\index\controller;


use app\index\Model\Category;
use app\index\Model\Sale;
use app\index\Model\Sale_item;
use app\index\Model\Stock_pile;
use think\Request;

class SaleController extends BaseController
{
    public function index(){
        $Sale = new Sale();
        $sale = $Sale->order('create_time','desc')->select();
        $this->assign('sale',$sale);
        return $this->fetch();
    }

    public function add(){
        $Category = new Category();
        $category = $Category->select();
        $this->assign('category',$category);
        return $this->fetch();
    }

    public function saleItem(){
        $Sale_item = new Sale_item();
        $item = $Sale_item->order('create_time','desc')->select();
        $this->assign('item',$item);
        return $this->fetch();
    }

    public function Item(){
        $id = Request::instance()->param('id/d');
        $Item = new Sale_item;
        $item = $Item->get($id);
        if (!is_null($item)){
            $sale = $item->sale()->where(['sale_item_id'=>$id])->select();
        }else{
            $this->error('数据不存在',url('index'));
        }
        $this->assign('sale',$sale);
        return $this->fetch();
    }

    public function addItem()
    {
        $requestdata = Request::instance()->param();
        if (is_null($requestdata)) {
            return json(['code' => '501', 'data' => 'error', 'msg' => '无提交内容']);
        } else {
            $max_item_id = Sale_item::max('id') + 1;
            $new_item = [];
            foreach ($requestdata as $key => $value) {
                foreach ($value as $k => $v) {
                    $new_item[$k]['number'] = $requestdata['number'][$k];
                    $new_item[$k]['price'] = $requestdata['price'][$k];
                    $new_item[$k]['quantity'] = $requestdata['quantity'][$k];
                    $new_item[$k]['quxiang'] = $requestdata['quxiang'][$k];
                }
            }
            $Sale = new Sale();
            $Stockpile = new Stock_pile();
            $maps = [];
            foreach ($new_item as $keys => $values) {
                $sale_stock = Stock_pile::get(['number' => $values['number'], 'status' => 0]);
                if ($sale_stock) {
                    $map['number'] = $sale_stock['number'];
                    $map['category_id'] = $sale_stock['category_id'];
                    $map['name'] = $sale_stock['name'];
                    $map['dimension'] = $sale_stock['dimension'];
                    $map['spec'] = $sale_stock['spec'];
                    $map['unit'] = $sale_stock['unit'];
                    $map['price'] = $values['price'];
                    $map['quantity'] = $values['quantity'];
                    $map['quxiang'] = $values['quxiang'];
                    $map['sale_item_id'] = $max_item_id;
                    $maps[] = $map;
//                    $Stockpile->saveAll($new_item);

                    if($sale_stock['quantity'] > $values['quantity']){
                        $Stockpile->validate('Instock')->where(['number'=>$values['number'],'status'=>0])->setDec('quantity', $values['quantity']);
                    }elseif($sale_stock['quantity'] == $values['quantity']){
                        try{
                            $Stockpile->validate('Instock')->where(['number'=>$values['number'],'status'=>0])->setDec('quantity', $values['quantity']);
                            $Stockpile->field(['number','status'])->where(['number'=>$values['number'],'status'=>0])->setField('status',1);
                        }catch (\Exception $e){
                            return json(['code' => '501', 'data' => 'error', 'msg' => $e->getMessage()]);
                        }

                    }else{
                        return json(['code' => '501', 'data' => 'error', 'msg' => '库存数量不足']);
                    }

                }else {
                    return json(['code' => '501', 'data' => 'error', 'msg' => '提交内容存在错误']);
                }
            }
            $insert_sale = $Sale->validate(true)->saveAll($maps);
            if (false === $insert_sale) {
                return json(['code' => '502', 'data' => 'error', 'msg' => $Sale->getError()]);
            }
            $data = [
                'pcs' => '',
                'amount' => '',
                'sale_id' => $max_item_id,
            ];
            $sale_item = new Sale_item();
            $sale_item->save($data);
            $sale = $sale_item->get($max_item_id);
            $pcs = $sale->sale()->where(['sale_item_id' => $max_item_id])->count();
            $amount = $sale->sale()->where(['sale_item_id' => $max_item_id])->sum('price');
            $sale_item->save([
                'pcs' => $pcs,
                'amount' => $amount],
                ['id' => $max_item_id]);

            return json(['code' => '1', 'data' => 'ok', 'msg' => '出库已完成']);
        }
    }

}